package com.rh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rh.entity.EduVideo;
import com.rh.feign_service.FeignEduService;
import com.rh.mapper.EduVideoMapper;
import com.rh.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程视频 服务实现类
 * @author rh
 * @since 2021-09-24
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    //注入feign
    @Autowired
    private FeignEduService feignEduService;
    //根据课程id删小节,并删除对应视频
    @Override
    public void removeVideoByCourseId(String courseId) {
        //根据课程id查询出视频
        QueryWrapper<EduVideo>wrapperVideo=new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        wrapperVideo.select("video_source_id");
        List<EduVideo> eduVideoList = baseMapper.selectList(wrapperVideo);
        //把List<Eduvideo>变成List<String>
        List<String> idList = new ArrayList<>();
        for(int i=0;i<eduVideoList.size();i++){
            EduVideo eduVideo = eduVideoList.get(i);
            String videoSourceId = eduVideo.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){ //如果不为空才添加
                //放到List<String>中
                idList.add(videoSourceId);
            }
        }
        if(idList.size()>0){ //如果idList集合中有值才删除
            //使用feign远程调用删除视频
            feignEduService.deleteBatchVideo(idList);
        }
        //删除小节
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
